package com.snj.visangedu;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout.LayoutParams;
import android.widget.MediaController;
import android.widget.VideoView;

public class Menu4Fragment extends Fragment implements OnClickListener {
	private final String TAG = "Test1Fragment";
	
	VideoView vdo_snu1st_0 = null;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View root = inflater.inflate(R.layout.fragment_main_menu4_0,
				container, false);
		return root;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		Log.d(TAG, "onActivityCreated");
		
		super.onActivityCreated(savedInstanceState);
		init_widget();
	}
	
	private void init_widget() {
		View view = getView();
//		mTxtCourseName = (TextView)
//				view.findViewById(R.id.todayplan_balancecoach_txt_coursename);
		
		vdo_snu1st_0 = (VideoView)view.findViewById(R.id.vdo_snu1st_0);
		vdo_snu1st_0.setOnCompletionListener(new OnCompletionListener() {
			
			@Override
			public void onCompletion(MediaPlayer mp) {
				// TODO Auto-generated method stub
				vdo_snu1st_0.start();
			}
		});
		
		
		
		MediaController mediaController0 = new MediaController(getActivity());
		
		mediaController0.setAnchorView(vdo_snu1st_0);
		
		Uri video = Uri.parse("http://viewnpop.com/snj/visangedu/VNP_PF2.mp4");
		
		vdo_snu1st_0.setMediaController(mediaController0);
		
		vdo_snu1st_0.setVideoURI(video);
		vdo_snu1st_0.setOnPreparedListener(PreparedListener);
		
		//menu2_videoview.requestFocus();
		
		vdo_snu1st_0.start();
		
	}
	
	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onClick(View v) {
	}
	
	
	private OnVideoSizeChangedListener onVideoSizeChangedListener =  
			new OnVideoSizeChangedListener() {  
			 public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {  
			  LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,  
			ViewGroup.LayoutParams.FILL_PARENT);  
			  vdo_snu1st_0.setLayoutParams(lp);
		 }  
	};
	
	MediaPlayer.OnPreparedListener PreparedListener = new OnPreparedListener() {
		
		@Override
		public void onPrepared(MediaPlayer m) {
			// TODO Auto-generated method stub
			
			try {
                if (m.isPlaying()) {
                    m.stop();
                    m.release();
                    m = new MediaPlayer();
                }
                
                m.setOnVideoSizeChangedListener(onVideoSizeChangedListener);  
                
                LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,  
              ViewGroup.LayoutParams.FILL_PARENT);  
                lp.topMargin = 16;
                vdo_snu1st_0.setLayoutParams(lp);
                vdo_snu1st_0.setMediaController(null);
                
                
                m.setVolume(0f, 0f);
                m.setLooping(false);
                m.start();
            } catch (Exception e) {
                e.printStackTrace();
            }   
		}
	};
}
